package com.net.test.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.net.test.service.NetService;
import com.net.test.vo.NetVO;

@Controller
public class NetController {
	private static final Logger logger = LoggerFactory.getLogger(NetService.class);
	@Autowired
	NetService netService;
	
	@SuppressWarnings("null")
	@RequestMapping(value = "/netList.ncb", method = {RequestMethod.GET,RequestMethod.POST})
    public String NetList(Locale locale, Model model) throws Exception {
        logger.info("Welcome home! The client locale is {}.", locale);  
        
        List<NetVO> list = (List<NetVO>)netService.selectData("testMapper.selectNet");        
      
//        NetVO vo = new NetVO();
//        
//        ArrayList<NetVO> arr = new ArrayList<NetVO>();
        
//        for(int i = 0; i < list.size(); i++)
//        {
//        	if(list.get(i).getBoard_Title().equals("kkk")){  
//        		vo.setBoard_Title(list.get(i).getBoard_Title());
//        		vo.setBoard_Contents(list.get(i).getBoard_Contents());
//        		
//        		arr.add(vo);
//        		
//        	}
//        }
       
        model.addAttribute("list" ,list);
        
        return "netList";           
        
	}
	
	@RequestMapping(value = "/netWrite.ncb", method = {RequestMethod.GET, RequestMethod.POST})
	public String NetWrite(Locale locale, Model model, HttpServletRequest request)throws Exception{
		logger.info("Welcome home! The client locale is {}.", locale);
		
		return "netWrite";
	}
	@RequestMapping(value = "/netInsert.ncb", method = {RequestMethod.GET, RequestMethod.POST})
	public String NetInsert(Locale locale, Model model, HttpServletRequest request)throws Exception{
		
		NetVO netVO = new NetVO();
		
		String title = request.getParameter("title");
		String contents = request.getParameter("contents");
		String boardname = "게시판";
		
		netVO.setBoard_Title(title);
		netVO.setBoard_Contents(contents);
		netVO.setBoard_Name(boardname);
		
		int result = netService.insertData("testMapper.insertNet", netVO);
		
		List<NetVO> list = (List<NetVO>)netService.selectData("testMapper.selectNet");
        System.out.println(list.size());
        
        model.addAttribute("list" ,list);
		
		return "netList";
		
	}
	@RequestMapping(value = "/netView.ncb", method = {RequestMethod.GET, RequestMethod.POST})
	public String NetView(Locale locale, Model model, HttpServletRequest request)throws Exception{
		
		String title = request.getParameter("title");
		NetVO view = (NetVO)netService.viewData("testMapper.netView", title);
		model.addAttribute("view", view);
		
		return "netView";
		
	}
	@RequestMapping(value = "/netDelete.ncb", method = {RequestMethod.GET, RequestMethod.POST})
	public String NetDel(Locale locale, Model model, HttpServletRequest request)throws Exception{
			
		
		String title = request.getParameter("del");
		NetVO delete = new NetVO();
		delete.setBoard_Title(title);		
								
		int result = netService.deleteData("testMapper.netDelete", delete);		
		
		List<NetVO> list = (List<NetVO>)netService.selectData("testMapper.selectNet");
		
		model.addAttribute("list", list);
		
		return "netList";		
	}
	@RequestMapping(value = "/netEdit.ncb", method = {RequestMethod.GET, RequestMethod.POST})
	public String NetEdit(Locale locale, Model model, HttpServletRequest request)throws Exception{
		
		
		
		String edit1 = request.getParameter("edit1");
		String edit2 = request.getParameter("edit2");
		
		NetVO edit = new NetVO();
		
		edit.setBoard_Title(edit1);
		edit.setBoard_Contents(edit2);
		
		model.addAttribute("edit", edit);
		
		return "netEdit";
		
	}
	@RequestMapping(value = "/netUpdate.ncb", method = {RequestMethod.GET, RequestMethod.POST})
	public String NetUpdate(Locale locale, Model model, HttpServletRequest request)throws Exception{
		
		String uptitle = request.getParameter("uptitle");
		String title = request.getParameter("title");
		String contents = request.getParameter("contents");
		
		NetVO update = new NetVO();
		
		update.setBoard_Title(title);
		update.setBoard_Contents(contents);
		update.setBoard_Name(uptitle);
		
		int result = netService.updateData("testMapper.updateNet", update);
		
		List<NetVO> list = (List<NetVO>)netService.selectData("testMapper.selectNet");
		
		model.addAttribute("list", list);
		
		return "netList";		
	}
	
	
}
